import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.event.*;
//import javax.mail.util.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.text.*;

public class analyser {
	
	public ArrayList<type> node=new ArrayList<type>();
	public int number=0;
    public 	static Message[] message;
	
	
	public analyser()
	{
		 //  String host = "pop.sina.com.cn"; 
		 //  String username = "wangq_30"; 
		 //  String password = "901170"; 
		  // int flag=0;
		 try{
		 //  Properties props = new Properties();
		 //  Session session = Session.getDefaultInstance(props, null);
		 //  Store store = session.getStore("pop3");
		  
		 //  store.connect(host, username, password);
		   
		   
		   //Folder folder = store.getFolder("INBOX");
		   //folder.open(Folder.READ_ONLY);
		   //message = folder.getMessages();
		   
		   //System.out.println("Messages's length: " + message.length);
		 //  ParseMimeMessage pmm = null;
		   
		   String str=new String();
		   //****
		   type rootnode = new type(null,false,0,"root");
		   node.add(rootnode);
		   //
		  BufferedReader in=new BufferedReader(new FileReader("name.txt"));
		  while((str=in.readLine())!=null)
		   {
		   
		    number=0;
		  //  pmm = new ParseMimeMessage((MimeMessage) message[i]);
		   // System.out.println("Message " + i + " subject: " + pmm.getSubject());
		  //  System.out.println("Message " + i + " sentdate: " + pmm.getSentDate());
		    int index=str.indexOf(";");
		    String subject=str.substring(0, index);
		    System.out.println(subject);
		    for(int j=0;j<subject.length();j++)
		    {
		    if(subject.charAt(j)=='/')
		    	number++;
		    }
		    
		    if(number==0)
		    	;//标题不符合要求的邮件不进行处理
		    else
		    {
		    	//对标题进行解析
		    	//System.out.println(number);
		    	String [] temp=("niho"+subject).split("/");
		    	for(int k=1;k<temp.length;k++)
		    	{
		    		if(k==1&&k==temp.length-1)
		    		{
		    			type type1=new type("root",true,k,temp[k]);
		    			if(lookup(type1,node)==-1)
		    			{
		    				node.add(type1);
		    				//****
			    			looktype("root").leaf.add(temp[k]);
		    			}
		    			
		    			
		    		}
		    		if(k==1&&k<temp.length-1)
		    		{
		    			type type2=new type("root",false,k,temp[k]);
		    			if(lookup(type2,node)==-1)
		    			{
		    				node.add(type2);
		    				//****
			    			looktype("root").leaf.add(temp[k]);
		    			}
		    			
		    		}
		    		if(k>1&&k<temp.length-1)
		    		{
		    			type type3=new type(temp[k-1],false,k,temp[k]);
		    			if(lookup(type3,node)==-1)
		    			{
		    				node.add(type3);
		    				//****
			    			looktype(temp[k-1]).leaf.add(temp[k]);
		    			}
		    			
		    		}
		    		if(k>1&&k==temp.length-1)
		    		{
		    			type type4=new type(temp[k-1],true,k,temp[k]);
		    			if(lookup(type4,node)==-1)
		    			{
		    				node.add(type4);
		    				//****
			    			looktype(temp[k-1]).leaf.add(temp[k]);
		    			}
		    			
		    		}
		    	}
		    }
		    
		   }
		  
		  
		 //  folder.close(true);
		   //store.close();
	}catch(Exception e){
		e.printStackTrace();
	}	  
	}
	
	//****
	public type looktype(String upname)
	{
		int i=-1;
		for(i=0;i<node.size();i++)
		{
			if(node.get(i).name.equals(upname))
			{
				return node.get(i);
			}
		}
		return node.get(0);
	}

	
	//查询某个名字的节点是否已存在
	public int lookup(type typ,ArrayList<type> n){
		int flag=-1;
		for(int i=0;i<n.size();i++)
		{
			if(n.get(i).is_end==typ.is_end&&n.get(i).level==typ.level&&n.get(i).name.equals(typ.name)&&n.get(i).uppername.equals(typ.uppername))
				{flag=i;break;}
		}
		return flag;
	}
	
	public static void main(String [] args)
	{
		analyser ana=new analyser();
		for(int i=0;i<ana.node.size();i++)
			System.out.println(ana.node.get(i).level+" "+ana.node.get(i).name+" "+ana.node.get(i).uppername+" "+ana.node.get(i).is_end);
	}
}
